package com.bnz.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bnz.entity.Classes;
import com.bnz.entity.Student;
import com.bnz.mapper.StudentMapper;
import com.bnz.service.IClassesService;
import com.bnz.service.IStudentService;
import com.bnz.vo.PageVo;
import com.bnz.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;


/**
 * @author xy
 * @date 2022/10/21
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {
    @Autowired
    private IClassesService classesService;


    @Override
    public List<Student> findAll() {
        return this.list();
    }


    @Override
    public PageVo<Student> search(StudentVo vo) {
        Page<Student> pageStu = this.query().like(!StringUtils.isEmpty(vo.getSname()), "sname", vo.getSname())
                .like(!StringUtils.isEmpty(vo.getAddr()), "addr", vo.getAddr())
                .eq(vo.getCid() != null && vo.getCid() != 0, "cid", vo.getCid())
                .page(new Page<>(vo.getPage(),vo.getPageSize()));
        for (Student st : pageStu.getRecords()) {
            Classes classes =classesService .getById(st.getCid());;
            st.setCname(classes.getCname());
        }
        return new  PageVo( pageStu.getTotal(),pageStu.getRecords());
    }


    @Override
    public void add(Student student) {

        this.save(student);

    }

    @Override
    public int update(Student student) {
        this.updateById(student);
        return 0;
    }

    @Override
    public void delete(String[] sid) {
        this.removeByIds(Arrays.asList(sid));
    }
}
